<template>
    <div>
        <Button v-if="showBackBtn" class="goBack" type="primary" @click="$router.back()"><Icon type="ios-arrow-back" /> 返回  </Button>
        <Card class="line-style">
            <div class="line-wrap">
                <div class="line-title">铁路</div>
                <div class="line-main">
                    <Form ref="formCustomRailway" :model="formCustomRailway" :rules="ruleCustom" :label-width="80">
                        <Row>
                            <Col span="7">
                                <FormItem label="出镜人数" prop="output_num">
                                    <Input type="number" number v-model="formCustomRailway.output_num" placeholder="请输入出镜人数">
                                        <span slot="append">人</span>
                                    </Input>
                                </FormItem>
                            </Col>
                            <Col span="7">
                                <FormItem label="入境人数" prop="input_num">
                                    <Input type="number" number v-model="formCustomRailway.input_num" placeholder="请输入入境人数">
                                        <span slot="append">人</span>
                                    </Input>
                                </FormItem>
                            </Col>
                            <Col span="7">
                                <Form-item label="上报日期" prop="imp_date">
                                    <DatePicker @on-change="dateChange" :options="options1" :value="formCustomRailway.imp_date"
                                                format="yyyy-MM-dd" type="date" placeholder="请选择上报日期"></DatePicker>
                                </Form-item>
                            </Col>
                        </Row>
                    </Form>
                    <div class="line-button" v-if="!showDetail">
                        <Button type="primary" @click="validateForm('formCustomRailway')">保存</Button>
                    </div>
                </div>
            </div>
        </Card>
        <Card class="line-style">
            <div class="line-wrap">
                <div class="line-title">公路</div>
                <div class="line-main">
                    <Form ref="formCustomMotorway" :model="formCustomMotorway" :rules="ruleCustom" :label-width="80">
                        <Row>
                            <Col span="7">
                                <FormItem label="出镜人数" prop="output_num">
                                    <Input type="number" number v-model="formCustomMotorway.output_num" placeholder="请输入出镜人数">
                                        <span slot="append">人</span>
                                    </Input>
                                </FormItem>
                            </Col>
                            <Col span="7">
                                <FormItem label="入境人数" prop="input_num">
                                    <Input type="number" number v-model="formCustomMotorway.input_num" placeholder="请输入入境人数">
                                        <span slot="append">人</span>
                                    </Input>
                                </FormItem>
                            </Col>
                            <Col span="7">
                                <Form-item label="上报日期" prop="imp_date">
                                    <DatePicker @on-change="dateChange" :options="options1" :value="formCustomMotorway.imp_date"
                                                format="yyyy-MM-dd" type="date" placeholder="请选择上报日期"></DatePicker>
                                </Form-item>
                            </Col>
                        </Row>
                    </Form>
                    <div class="line-button" v-if="!showDetail">
                        <Button type="primary" @click="validateForm('formCustomMotorway')">保存</Button>
                    </div>
                </div>
            </div>
        </Card>
        <Card class="line-style">
            <div class="line-wrap">
                <div class="line-title">机场</div>
                <div class="line-main">
                    <Form ref="formCustomAirport" :model="formCustomAirport" :rules="ruleCustom" :label-width="80">
                        <Row>
                            <Col span="7">
                                <FormItem label="出镜人数" prop="output_num">
                                    <Input type="number" number v-model="formCustomAirport.output_num" placeholder="请输入出镜人数">
                                        <span slot="append">人</span>
                                    </Input>
                                </FormItem>
                            </Col>
                            <Col span="7">
                                <FormItem label="入境人数" prop="input_num">
                                    <Input type="number" number v-model="formCustomAirport.input_num" placeholder="请输入入境人数">
                                        <span slot="append">人</span>
                                    </Input>
                                </FormItem>
                            </Col>
                            <Col span="7">
                                <Form-item label="上报日期" prop="imp_date">
                                    <DatePicker @on-change="dateChange" :options="options1" :value="formCustomAirport.imp_date"
                                                format="yyyy-MM-dd" type="date" placeholder="请选择上报日期"></DatePicker>
                                </Form-item>
                            </Col>
                        </Row>
                    </Form>
                    <div class="line-button" v-if="!showDetail">
                        <Button type="primary" @click="validateForm('formCustomAirport')">保存</Button>
                    </div>
                </div>
            </div>
        </Card>
        <Card class="line-style">
            <div class="line-wrap">
                <div class="line-title">口岸</div>
                <div class="line-main">
                    <Form ref="formCustomPort" :model="formCustomPort" :rules="ruleCustom" :label-width="80">
                        <Row>
                            <Col span="7">
                                <FormItem label="出镜人数" prop="output_num">
                                    <Input type="number" number v-model="formCustomPort.output_num" placeholder="请输入出镜人数">
                                        <span slot="append">人</span>
                                    </Input>
                                </FormItem>
                            </Col>
                            <Col span="7">
                                <FormItem label="入境人数" prop="input_num">
                                    <Input type="number" number v-model="formCustomPort.input_num" placeholder="请输入入境人数">
                                        <span slot="append">人</span>
                                    </Input>
                                </FormItem>
                            </Col>
                            <Col span="7">
                                <Form-item label="上报日期" prop="imp_date">
                                    <DatePicker @on-change="dateChange" :options="options1" :value="formCustomPort.imp_date"
                                                format="yyyy-MM-dd" type="date" placeholder="请选择上报日期"></DatePicker>
                                </Form-item>
                            </Col>
                        </Row>
                    </Form>
                    <div class="line-button" v-if="!showDetail">
                        <Button type="primary" @click="validateForm('formCustomPort')">保存</Button>
                    </div>
                </div>
            </div>
        </Card>
        <Modal v-model="modal" title="确认操作" width="20" @on-ok="handleSubmit">
            <div style="text-align:center">
                <p>是否确认保存!</p>
            </div>
        </Modal>
    </div>
</template>

<script>
import { insert, findList, update } from '@/api/outbreak-management/city-operation/transportation/Immigration'
// 城市运行-出入境情况添加
export default {
  data () {
    const validateInt = (rule, value, callback) => {
      if (value % 1 !== 0) {
        callback(new Error('请输入整数'))
      } else {
        callback()
      }
    }
    return {
      options1: { // 不可选今天之后的日期
        disabledDate (date) {
          return date && date.valueOf() > Date.now()
        }
      },
      // 保存状态码
      state: '',
      // 提示框
      modal: false,
      // 控制保存按钮显隐
      showDetail: false,
      // 控制返回按钮显隐
      showBackBtn: false,
      // 添加表单/修改表单
      formCustomRailway: {
        type: '0',
        output_num: '',
        input_num: '',
        imp_date: ''
      },
      formCustomMotorway: {
        type: '1',
        output_num: '',
        input_num: '',
        imp_date: ''
      },
      formCustomAirport: {
        type: '2',
        output_num: '',
        input_num: '',
        imp_date: ''
      },
      formCustomPort: {
        type: '3',
        output_num: '',
        input_num: '',
        imp_date: ''
      },
      ruleCustom: {
        output_num: [
          { required: true, type: 'number', message: '出境人数必填', trigger: 'blur' },
          { min: 0, max: 99999999, type: 'number', message: '出境人数范围0-99999999', trigger: 'blur' },
          { validator: validateInt, trigger: 'blur' }
        ],
        input_num: [
          { required: true, type: 'number', message: '入境人数必填', trigger: 'blur' },
          { min: 0, max: 99999999, type: 'number', message: '入境人数范围0-99999999', trigger: 'blur' },
          { validator: validateInt, trigger: 'blur' }
        ],
        imp_date: [
          { required: true, message: '上报日期必填', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    validateForm (name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          if (name === 'formCustomRailway') {
            this.state = '0'
          } else if (name === 'formCustomMotorway') {
            this.state = '1'
          } else if (name === 'formCustomAirport') {
            this.state = '2'
          } else if (name === 'formCustomPort') {
            this.state = '3'
          }
          this.modal = true
        } else {
          this.$Message.error('表单验证失败,请检查后再试!')
        }
      })
    },
    /**
             * 日期格式化
             */
    dateChange (date) {
      this.formCustomRailway.imp_date = date
      this.formCustomMotorway.imp_date = date
      this.formCustomAirport.imp_date = date
      this.formCustomPort.imp_date = date
    },
    handleSubmit () {
      let requestData
      if (this.state === '0') {
        requestData = this.formCustomRailway
      } else if (this.state === '1') {
        requestData = this.formCustomMotorway
      } else if (this.state === '2') {
        requestData = this.formCustomAirport
      } else if (this.state === '3') {
        requestData = this.formCustomPort
      }
      if (requestData.id) {
        update(requestData).then(({ data }) => {
          let { errcode } = data
          if (errcode === 0) {
            this.$Message.success('保存成功!')
          } else {
            this.$Message.error('保存失败!')
          }
        })
      } else {
        insert(requestData).then(({ data }) => {
          let { errcode } = data
          if (errcode === 0) {
            this.$Message.success('保存成功!')
          } else {
            this.$Message.error('保存失败!')
          }
        })
      }
    },
    isEditStatus () {
      if (this.$route.query && this.$route.query.imp_date) {
        console.log(this.$route.query.imp_date)
        if (this.$route.query.showDetail) {
          this.showDetail = true
        }
        this.showBackBtn = true
        this.getEditData(this.$route.query.imp_date)
      }
    },
    getEditData (imp_date) {
      findList({ imp_date }).then(({ data }) => {
        let { errcode, data: res } = data
        if (errcode === 0) {
          res.forEach(item => {
            item.imp_date = this.$common.moment(item.imp_date).format('YYYY-MM-DD')
            if (item.type === '0') {
              this.formCustomRailway = item
            } else if (item.type === '1') {
              this.formCustomMotorway = item
            } else if (item.type === '2') {
              this.formCustomAirport = item
            } else if (item.type === '3') {
              this.formCustomPort = item
            }
          })
        }
      })
    }
  },
  created () {
    this.isEditStatus()
  },
  mounted () {
  }
}
</script>
<style scoped lang="less">
</style>
